﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
//using System.Web.Mail;

namespace E1_eMart
{
    public partial class ForgetInfoAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            if (Request["id_Email"] == null)
                return;         
            string toEmail = Request["id_Email"];          
            if (toEmail.Equals(""))
                return;
            TaiKhoan tk = GetTaiKhoanByEmail(toEmail);
            if (tk == null)
                return;
            mail.From = new MailAddress("emart.bid@gmail.com");
            //m.To.Add(new MailAddress("YourEmail@domain.com"));
            mail.To.Add(new MailAddress(toEmail));
            mail.Subject = "Restore Account";
            mail.IsBodyHtml = true;
            mail.Body = "ID: " + tk.TenTaiKhoan + "\n" +
                        "Password: " + tk.MatKhau;
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 25;
            smtp.Credentials = new System.Net.NetworkCredential("emart.bid@gmail.com", "18031990");
            smtp.EnableSsl = true;
            try
            {
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                Response.Write("Exception caught in CreateTimeoutTestMessage(): " + ex.Message);
            }
        }

        private TaiKhoan GetTaiKhoanByEmail(string strEmail)
        {
            DataClasses1DataContext edata = new DataClasses1DataContext();
            try
            {
                ThongTinTaiKhoan tttk = edata.ThongTinTaiKhoans.Single<ThongTinTaiKhoan>(temp => temp.Email.Equals(strEmail.Trim()));
                int matk = int.Parse(tttk.MaTaiKhoan.ToString());
                if (matk <= 0)
                    return null;
                TaiKhoan tk = edata.TaiKhoans.Single<TaiKhoan>(temp => temp.MaTaiKhoan == matk);
                return tk;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}